<?php

class m110908_030323_create_blacklist extends CDbMigration {

	protected $_tables = array(
		'blacklist'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'type'=>'string',
			'value'=>'string',
		),
	);
	protected $_indexes = array(
		'blacklist'=>array(
			'type',
			'value',
		),
	);

	public function safeUp() {
		foreach ($this->_tables as $key=>$value) {
			$this->createTable($key, $value);
		}

		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->createIndex($name, $table, $column);
			}
		}

		$sql = 'insert into blacklist (insertTime, updateTime, type, value) ';
		$sql .= 'values(:insertTime, :updateTime, :type, :value);';
		$cmd = $this->dbConnection->createCommand($sql);
		$time = time();
		$cmd->bindParam(":insertTime", $time, PDO::PARAM_INT);
		$cmd->bindParam(":updateTime", $time, PDO::PARAM_INT);
		$type = News::TYPE_WEB;
		$cmd->bindParam(":type", $type, PDO::PARAM_STR);
		$value = 'test';
		$cmd->bindParam(":value", $value, PDO::PARAM_STR);
		$cmd->execute();

		$auth = Yii::app()->authManager;

		// News addition
		$auth->createOperation('newsBlacklist', 'Blacklist news sender');
		$auth->addItemChild('newsModerator', 'newsBlacklist');

		// Blacklist Operation
		$auth->createOperation('blacklistList', 'List blacklist');
		$auth->createOperation('blacklistView', 'View blacklist');
		$auth->createOperation('blacklistCreate', 'Create blacklist');
		$auth->createOperation('blacklistUpdate', 'Update blacklist');
		$auth->createOperation('blacklistDelete', 'Delete blacklist');
		$auth->createOperation('blacklistManage', 'Manage blacklist');

		// Blacklist Moderator
		$role = $auth->createRole('blacklistModerator', 'Blacklist Moderator');
		$role->addChild('blacklistList');
		$role->addChild('blacklistView');
		$role->addChild('blacklistCreate');
		$role->addChild('blacklistUpdate');
		$role->addChild('blacklistDelete');
		$role->addChild('blacklistManage');

		$auth->addItemChild('admin', 'blacklistModerator');
	}

	public function safeDown() {
		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->dropIndex($name, $table, $column);
			}
		}

		foreach ($this->_tables as $key=>$value) {
			$this->dropTable($key);
		}

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'blacklistModerator');

		$auth->removeItemChild('blacklistModerator', 'blacklistList');
		$auth->removeItemChild('blacklistModerator', 'blacklistView');
		$auth->removeItemChild('blacklistModerator', 'blacklistCreate');
		$auth->removeItemChild('blacklistModerator', 'blacklistUpdate');
		$auth->removeItemChild('blacklistModerator', 'blacklistDelete');
		$auth->removeItemChild('blacklistModerator', 'blacklistManage');

		$auth->removeAuthItem('blacklistList');
		$auth->removeAuthItem('blacklistView');
		$auth->removeAuthItem('blacklistCreate');
		$auth->removeAuthItem('blacklistUpdate');
		$auth->removeAuthItem('blacklistDelete');
		$auth->removeAuthItem('blacklistManage');

		$auth->removeAuthItem('blacklistModerator');

		$auth->removeItemChild('newsModerator', 'newsBlacklist');
		$auth->removeAuthItem('newsBlacklist');
	}

}